$(function () {
    /*
    // Пример рабочей гриды
    $("#grid").jqGrid({
        url: basic_path + "/core/login/asd",
        datatype: "json",
        mtype: "GET",
        colNames: ["Inv No", "Date", "Amount", "Tax", "Total", "Notes+"],
        colModel: [
            { name: "invid", width: 55 },
            { name: "invdate", width: 90 },
            { name: "amount", width: 80, align: "right" },
            { name: "tax", width: 80, align: "right" },
            { name: "total", width: 80, align: "right" },
            { name: "note", width: 150, sortable: false }
        ],
        pager: "#pager",
        rowNum: 10,
        rowList: [1, 20, 300],
        sortname: "invid",
        sortorder: "desc",
        viewrecords: true,
        gridview: true,
        autoencode: true,
        caption: "My first grid",
        width: 660
    });
    */
});



function deleteItem( entity, id, grid_unit,unit ){

    $.post(basic_path + "/core/dbapi/delete", {
        entity:entity,
        id : id,
        unit: unit
    }, function (responceobj, status) {

        if (status == 'success') {
            if(responceobj.responcestatus == 'ok'){
                jQuery("#" + grid_unit + "_grid").trigger('reloadGrid');
                jQuery("#" + grid_unit + "_grid").trigger('reloadGrid');
            }

        } else {
            //ActionDialog("Cant pick products", "alert");
        }
    }, 'json');

}

function toggleVisibility( entity, id, grid_unit,unit ){

    $.post(basic_path + "/core/dbapi/togglephotovisibility", {
        entity:entity,
        id : id,
        unit: unit
    }, function (responceobj, status) {

        if (status == 'success') {
            if(responceobj.responcestatus == 'ok'){
                jQuery("#" + grid_unit + "_grid").trigger('reloadGrid');
                jQuery("#" + grid_unit + "_grid").trigger('reloadGrid');
            }

        } else {
            //ActionDialog("Cant pick products", "alert");
        }
    }, 'json');

}

jQuery.extend($.fn.fmatter , {
    imageFmatter : function(cellvalue, options, rowdata) {
        //return "$"+cellvalue;
        return "<img class='smm-picto' src='"+ cellvalue +"' />"
    }
});

jQuery.extend($.fn.fmatter.imageFmatter , {
    unformat : function(cellvalue, options) {

        return cellvalue;
        //return cellvalue.replace("$","");
    }
});

function imageElem (value, options) {

    //TODO откуда брать value?

     var el = document.createElement("img");
     el.class = "popup-img";
     el.src = getSelectedCellImageSrc();

    return el
}

function imageValue(elem, operation, value) {

    if(operation === 'get') {
        return $(elem).attr('src')

    } else if(operation === 'set') {

        setTimeout(function(){
            var src = getSelectedCellImageSrc()

            $(elem).attr('src',src)
        },2000)



    }
}

function getSelectedCellImageSrc(){
    var name = $('input[name=grid_name]').val();
    var grid = jQuery('#' + name);
    var sel_id = grid.jqGrid('getGridParam', 'selrow');

    var src = $('tr[id='+sel_id+']').find('img').attr('src');

    return src
}
